package cn.wxz1997.permission.controller;

import cn.wxz1997.permission.dto.UserForm;
import cn.wxz1997.permission.model.User;
import cn.wxz1997.permission.service.UserService;
import cn.wxz1997.permission.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description: ${todo}
 * @Author: wxz1997
 * @Date 18-8-4下午9:55
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result<List<User>> list(){
        return Result.success(userService.list());
    }

    @GetMapping("/{id}")
    public Result selectOne(@PathVariable Integer id){
        return Result.success(userService.selectById(id));
    }

    @PostMapping
    public Result<User> add(@RequestBody UserForm userForm){
        User resultUser = userService.add(userForm);
        if (resultUser == null){
            return Result.error("插入失败");
        }else {
            return Result.success(resultUser);
        }
    }

    @PutMapping("/{id}")
    public Result<User> update(@RequestBody UserForm userForm){
        User resultUser = userService.update(userForm);
        if (resultUser == null){
            return Result.error("更新失败");
        }else {
            return Result.success(resultUser);
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(userService.delete(id));
    }
}
